<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\VillageThingsComment;
use App\Models\VillageThings;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Show;
use Dcat\Admin\Controllers\AdminController;

use \App\Models\VillageThingsComment as VillageThingsCommentModel;

class VillageThingsCommentController extends AdminController
{
    public function index(Content $content)
    {
        $things = VillageThings::find(request()->things_id);
        return $content
            ->header('大事小情评论')
            ->description($things ? trunc_title($things->content, 30) : '')
            ->breadcrumb('评论')
            ->body($this->grid());
    }
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(VillageThingsCommentModel::with(['commenter', 'replyer']), function (Grid $grid) {

            $grid->model()->where('village_things_id', request()->things_id);
            $grid->model()->orderBy('id', 'desc');

            $grid->column('id')->sortable();
            $grid->column('commenter', '发布者') ->display(function($commenter){
                return ($commenter['real_name'] ? $commenter['real_name'] : $commenter['user_name']) . ($this->is_expert ? '（专家）' : '');
            });
            $grid->column('content')->display(function ($content) {

                if ($this->reply_user_id) {
                    $repler_name = ($this->replyer['real_name'] ? $this->replyer['real_name'] : $this->replyer['user_name']);
                    $content = '回复 ' . '<a style="color:#495abf;">' . $repler_name . '</a>：' . $content;
                }
                return $content;
            })->width('40%');

            $grid->column('created_at', '评论时间');
            $grid->column('zan_num', '点赞数');
            $grid->column('is_show', '展示')->switch();

            $grid->quickSearch(['commenter.real_name', 'commenter.user_name', 'replyer.real_name', 'replyer.user_name', 'content'])->placeholder('搜索...');

            $list_url = admin_base_path('village_things');
            $list_btn_html = '<a href="'.$list_url.'" class="btn btn-primary btn-outline pull-right" style="margin-right: 10px;"><span class="d-none d-sm-inline">返回列表</span></a >';
            $grid->tools($list_btn_html);

            $grid->disableEditButton();
            $grid->disableViewButton();
            $grid->disableCreateButton();
            $grid->enableDialogCreate();

        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new VillageThingsComment(), function (Show $show) {
            $show->field('id');
            $show->field('village_things_id');
            $show->field('content');
            $show->field('imgs');
            $show->field('video');
            $show->field('user_id');
            $show->field('zan_num');
            $show->field('reply_id');
            $show->field('reply_user_id');
            $show->field('is_show');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new VillageThingsComment(), function (Form $form) {
            $form->display('id');
            $form->text('village_things_id');
            $form->text('content');
            $form->text('imgs');
            $form->text('video');
            $form->text('user_id');
            $form->text('zan_num');
            $form->text('reply_id');
            $form->text('reply_user_id');
            $form->text('is_show');
        
            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
